#!/bin/env

from abstractprocess import AbstractPorcess
import os
import json
import sys
import copy
import commands
import log

class ProcessFile(AbstractPorcess):
    def init(self,config_file,parameter):
        self.conf= self.read_config(config_file)
        self.parameter = parameter
        #print parameter
        #print self.conf
        #print parameter[3]
        if parameter[3] in self.conf:
            #print self.conf
            self.module = self.conf[parameter[3]]
        else:
            print parameter[3],"not in process config file"
            sys.exit(-1)

    def process(self,reader,writer):
        task = self.new_instance(self.module['task'].lower(),self.module['task'])
        task.init(self.parameter)
        output_dict = {}
        from time import time
        start=time()
        for line in reader.read():
            output_dict = task.line_process(self.module,writer,line)
        task.finish_process(self.module,writer)
        reader.close()
        writer.close()
        finish=time()
        log.info("process time is : %s" ,(finish-start))
        #instance = self.new_instance(self.module['task'].lower(),self.module['task'])
        #self.call_instance_function(instance,'test',aa="123",bb="u")

    def abc(self):
        print "haha"
